package com.zatech.cgnci.project.controller;


import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zatech.cgnci.project.base.enums.ReserveTypeEnum;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.dto.CgnciUnearnedPremiumReserveLedgerDTO;
import com.zatech.cgnci.project.service.CgnciUnearnedPremiumReserveLedgerService;
import com.zatech.cgnci.project.vo.CgnciRiskVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.zatech.cgnci.project.vo.CgnciUnearnedPremiumReserveLedgerVO;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 * 未到期责任准备金台账表 前端控制器
 * </p>
 *
 * @author Generator
 * @since 2025-01-23
 */
@RestController
@RequestMapping("/unearned-premium-reserveLedger")
@Api(produces = "application/json", tags = "未到期责任准备金台账表")
public class CgnciUnearnedPremiumReserveLedgerController {

    @Autowired
    private CgnciUnearnedPremiumReserveLedgerService reserveLedgerService;
    @PostMapping("/page")
    @ApiOperation(value = "分页列表查询")
    public IPage<CgnciUnearnedPremiumReserveLedgerVO> page(@RequestBody CgnciUnearnedPremiumReserveLedgerDTO queryDTO) {
        return reserveLedgerService.pageData(queryDTO);
    }

    @GetMapping("/exportAll")
    @ApiOperation(value = "准备金台账文件下载")
    public void exportProjectAll(HttpServletResponse response,CgnciUnearnedPremiumReserveLedgerDTO queryDTO){
        try(OutputStream outputStream = response.getOutputStream();
            ExcelWriter writer = reserveLedgerService.export(queryDTO);) {
            response.setContentType("application/x-download");
            String fileName = URLEncoder.encode(ReserveTypeEnum.getEnumByCode(queryDTO.getReserveTypeCode()).getName()+".xlsx", "UTF-8");
            response.setCharacterEncoding("UTF-8");
            response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            writer.getWorkbook().write(outputStream);
            response.getOutputStream().flush();
        }catch (Exception e){
            throw new GlobalException(ResponseCodeEnum.PARAMS_ILLEGAL, "下载文件失败");
        }
    }

}

